﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SmartCity.Shared;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace CommonManagement
{
    /// <summary>
    /// 新闻应用服务类
    /// </summary>
    public class PressAppService : ApplicationService, IPressAppService
    {
        private readonly IRepository<Press, int> _pressRepository;
        private readonly IRepository<Comment, int> _commentRepository;

        public PressAppService(
            IRepository<Press, int> pressRepository,
            IRepository<Comment, int> commentRepository)
        {
            _pressRepository = pressRepository;
            _commentRepository = commentRepository;
        }

        // 分页列表
        public async Task<ListResponse<PressDto>> PagedListAsync(PagedListRequest request,
            string hot = null,
            string publishDate = null,
            string title = null,
            string top = null,
            string type = null)
        {
            // 数据筛选
            var query = _pressRepository
                .WhereIf(!string.IsNullOrWhiteSpace(hot), m => m.Hot == (SystemWhether)Enum.Parse(typeof(SystemWhether), hot))
                .WhereIf(!string.IsNullOrWhiteSpace(publishDate), m => m.PublishDate == DateTime.Parse(publishDate))
                .WhereIf(!string.IsNullOrWhiteSpace(title), m => m.Title.Contains(title))
                .WhereIf(!string.IsNullOrWhiteSpace(top), m => m.Top == (SystemWhether)Enum.Parse(typeof(SystemWhether), top))
                .WhereIf(!string.IsNullOrWhiteSpace(type), m => m.Type == int.Parse(type));
            // 计数
            var count = query.Count();
            // 排序
            if (!string.IsNullOrWhiteSpace(request.OrderByColumn))
            {
                var orderColumns = request.OrderByColumn.Split(',');
                foreach (var column in orderColumns)
                {
                    query = query.OrderBy(column);
                }
            }
            // 分页
            query = query.Page(request.PageNum, request.PageSize);
            // 评论计数
            int pressId;
            var list = await query.ToListAsync();
            for (int i = 0; i < list.Count; i++)
            {
                pressId = list[i].Id;
                list[i].CommentNum = await _commentRepository.CountAsync(m => m.NewsId == pressId);
            }
            // 返回结果
            var dtos = ObjectMapper.Map<List<Press>, List<PressDto>>(list);
            return ListResponse<PressDto>.Successful(count, dtos);
        }

        // 详情
        public async Task<ResultResponse<PressDto>> GetAsync(int id)
        {
            var entity = await _pressRepository.GetAsync(id);
            entity.Read();
            entity.CommentNum = await _commentRepository.CountAsync(m => m.NewsId == id);
            var dto = ObjectMapper.Map<Press, PressDto>(entity);
            return ResultResponse<PressDto>.Successful(dto);
        }

        // 点赞
        public async Task<ActionResponse> LikeAsync(int id)
        {
            var entity = await _pressRepository.GetAsync(id);
            entity.Like();
            // 返回结果
            return ActionResponse.Successful();
        }
    }
}